const app = getApp()
Page({
  data: {
    domainName: app.domainName,
    inputPhone:'',
    sexid:0,
    sexArr:['球员性别','男', '女'],
    age:'',
    theAva:app.domainName+'tu/reg-tu.png',
    hasAvatar:false
  },
  setinput(e) {
    this.setData({inputPhone:  e.detail.value});
  },
  setsex(e){
    this.setData({sexid:  e.detail.value});
  },
  onLoad() {
   
  },
  formSubmit(e) {
    console.log(e.detail.value)
    var _phone=e.detail.value.phone;
    var _name=e.detail.value.name;
    var _sexid=this.data.sexid;
    var _qiuyi=e.detail.value.qiuyi;
    if('' == _phone){
      this.popMsg("请输入手机号");
      return;
    }
    if(_phone.length < 9){
      this.popMsg("请输正确的手机号");
      return;
    }
    var _phoneArr=_phone.split("");
    console.log(_phoneArr)
    if(_phoneArr[0] == _phoneArr[1] && _phoneArr[0] == _phoneArr[2]){
      this.popMsg("请输正确的手机号");
      return;
    }
    if('' == _name){
      this.popMsg("请输入球员姓名");
      return;
    }
    if(0 == _sexid){
      this.popMsg("请输入球员性别");
      return;
    }
    if('' == e.detail.value.gao){
      this.popMsg("请输入球员身高");
      return;
    }
    if('' == e.detail.value.weight){
      this.popMsg("请输入球员体重");
      return;
    }
    if('' == e.detail.value.age){
      this.popMsg("请输入球员年龄");
      return;
    }
    if('' == _qiuyi){
      this.popMsg("请输入球衣尺码");
      return;
    }
    if('' == e.detail.value.idcard){
      this.popMsg("请输入球员身份证号码");
      return;
    }
    if(e.detail.value.idcard.length < 14 ){
      this.popMsg("请输入正确球员身份证号码");
      return;
    }
    
    if(e.detail.value.age < 1 || e.detail.value.age > 99){
      this.popMsg("请输入正确球员身份证号码");
      return;
    }
    if(false == this.data.hasAvatar){
      this.popMsg("请上传2寸照片");
      return;
    }
    wx.request({
      url: app.domainName + 'web/?c=setuser',
      method: 'POST',
      data: {openid:app.globalData.openid,phone:_phone, name:_name,sex:_sexid,gao:e.detail.value.gao,
        weight:e.detail.value.weight,age:e.detail.value.age,idcard:e.detail.value.idcard, qiuyi:_qiuyi
      }, header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {  
        wx.redirectTo({url: '/pages/welcome/index'});
        return;
      }});
    


  },
  setage(e){
    var _idcard=  e.detail.value;
    if(_idcard.length > 13){
      var myDate = new Date();
      var month = myDate.getMonth() + 1;
      var day = myDate.getDate();
      var age = myDate.getFullYear() - _idcard.substring(6, 10) - 1;
      if (_idcard.substring(10, 12) < month || _idcard.substring(10, 12) == month && _idcard.substring(12, 14) <= day) {
        age++;
      }
      this.setData({age:age});
    }

  },
  setavatar(){
    var _this=this;
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success (res) {
         var _tempFilePaths = res.tempFilePaths;
         _this.setData({theAva: _tempFilePaths});
         wx.uploadFile({
          url: app.domainName + 'web/?c=uploadavatar',
          filePath: _tempFilePaths[0],
          name: 'file',
          formData: {
            'op':  app.globalData.openid
          },
          success (res){
             if('ok' == res.data){
               _this.setData({hasAvatar:true});
             }
          }
        })

      }
    })
  },
  popMsg(msg){
    wx.showToast({
      title: msg,
      icon: 'none',
      duration: 1500
    })
  }
})
